define(['jquery','api','user','util','text!pages/order/history.html','Vue'],function($,api,user,util){
	var vm,
	   time,
	   startid,
	   controller = {
	   init:function(id){
	   	   if(!user.checkLogin())return;
		   api.initFooter(3);
		   api.setTitle('交易历史');	
		   if(!vm){
		     this.render();
		   }else{
		     vm.show=true;
		   }
		   this.loadNow();
	   },
	   render:function(){
	   	  var that=this;
	   	   $('#pages').append('<div id="orderHistrory"></div>');
	       require(['text!pages/order/history.html','Vue','util'],function(tpl,Vue,util){
	            vm=new Vue({
	               el:'#orderHistrory',
	               data:{
	               	  show:true,
	               	  ordersHistory:[], // 历史列表数据容器
	               	  ordersToday:[], // 今天列表数据容器
	               	  loading:true,
                            startTime:(function(d){return d.format('yyyy-MM-dd')})(new Date()),
                            endTime:(function(d){return d.format('yyyy-MM-dd')})(new Date()),
                            isToday:true, // tab 是否为今天，否则历史
                            showMore:{ // 显示更多
                                  today:false,
                                  history:false
                            }
	               },
	               filters:{
	                 lowHigh:util.lowHigh, //方向
                        //金额格式化
                        priceFormat:function(v,mindecimal){
                        	if(isNaN(v)){return v=''}; // 20161209dyx
                            return util.priceFormat(mindecimal,v);
                        }
	              },
	               methods:{
	                 showDetail:function(opensn,o){
	                 	api.setOrder(o);
				 	    location.hash="#/order/historydetail/"+opensn; // 20161209dyx
				 	 },
				 	 query:function(more){
				 	    controller.query(more);
				 	 },
				 	 switchTab:function(t){
		              	 this.isToday=t;
		             }
	               },
	               template:tpl
	            });
	       })
	   },
	   query:function(more){
	   	
                if(more === undefined){
                	vm.ordersHistory=[];
                    startid=0;
                }
                time=vm.startTime;
                this.load();
	   },
	   load:function(){
	   		vm.loading = true;
            var orders;
	   	   var params={startday:time,endday:time,id:startid,count:20,sorttype:49};
	   	   api.historyOrders(params,function(rs){
                        vm.loading = false;
                       if(rs.retcode=='0' && rs.data){
                            orders=rs.data;
                            vm.ordersHistory=vm.ordersHistory.length?vm.ordersHistory.concat(orders):orders;
                            var len = orders.length;
                            startid+=len;
                            if(len>=20){
                               vm.showMore.history=true;
                            }else{
                               vm.showMore.history=false;
                            }
                       }else{
                       	util.msg('暂无更多历史数据');
                       }
	   	   })
	   },
	   loadNow:function(){
	   	  var params={id:startid,count:20,sorttype:49};
	      api.closeOrders(params,function(rs){
	         vm.loading = false;
	         if(rs.retcode=='0' && rs.data){
		         vm.ordersToday=rs.data;
	      	}else{
	      		util.msg('暂无更多今日数据');
	      	}
	      })
	      
	   },
	   onRouteChange:function(){
	       if(vm)vm.show=false;
	   }
	};
    return controller;
})